package
{
	import as3isolib.display.IsoSprite;
	
	import flash.display.Sprite;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import flashx.textLayout.formats.Float;

	/**
	 * A Building object encapsulates all the information important to a single grid space
	 * occupied by a building; such that if a building covers more than one tile, it will
	 * have a Building object for each tile. Only one Building object of this actual 'building'
	 * will control the logic; this is decided by the 'main' attribute of the object.
	 */
	public class Building
	{
		public var hasCar:Boolean = false;
		public var x: int;
		public var y: int;
		private var type:String;
		private var main:Boolean = true;
		public var image:Animation;
		
		private var prodRate : Number;
		public var producing : Boolean = false;
		private var resource : Number = 0;
		private var resourceCap : Number = 500;
		private var buildingLevel : int = 1;
		private var levelSprite : Sprite = new Sprite();
		private var lv : TextField;
		

		/**
		 * Creates a new Building tile as part of a larger building.
		 * 
		 * @param xPos: the x co-ordinate of the tile
		 * @param yPos: the y co-ordinate of the tile
		 * @param productionRate: The amount generated by this building per frame (where fps is decided by the Render.)
		 * @param name: the name of the type of building this is
		 * @param im: the animated image representing this building's graphics
		 */
		public function Building(xPos:int,yPos:int,productionRate:Number,name:String,im:IsoSprite){
			image=im as Animation;
			this.x = xPos;
			this.y = yPos;
			this.prodRate = productionRate;
			this.type = name;
			createLevelSprite();
		}
		
		/**
		 * Generates the resource of this building's type, and stores it in the building's stash.
		 */
		public function generateResources():void{
			if(resource < resourceCap){
				resource += prodRate;
				if(resource > resourceCap) resource = resourceCap;
			}
		}
		
		/**
		 * Resets the amount of resource generated by this building to zero,
		 * and returns how much had been generated.
		 */
		public function collectResources():int{
			var x : int = resource;
			resource = 0;
			return x;
		}
		
		public function getType():String{
			return type;
		}
		
		public function setMain(t:Boolean):void{
			main=t;
		}
		
		/**
		 * Returns true if this is the main tile of the building object in charge of the building's logic.
		 */
		public function isMain():Boolean{
			return main;
		}
		public function getResource():Number{
			return resource;
		}
		public function getLevel():int{
			return buildingLevel;
		}
		public function levelUp():void{
			buildingLevel+=1;
			lv.text=""+buildingLevel;
			resourceCap*=1.5;
		}
		private function createLevelSprite():void{
			lv = Formatting.newSubtitle(buildingLevel+"");
			levelSprite.addChild(lv);
		}
		
		public function getLevelSprite():IsoSprite{
			var i : IsoSprite = new IsoSprite();
			i.sprites = new Array(levelSprite);
			i.x = (x-1)*Render.gridWidth;
			i.y = (y-1)*Render.gridWidth;
			return i;
		}

	}
}